package component

import (
	"core/service"
	"game_flee/component/base"
	"game_flee/component/gameFlee"
	"game_flee/models/request"
	"hall/models/response"
	"sync"
)

type FleeKillRoom struct {
	sync.RWMutex
	ID         uint                            `json:"id"`
	Name       string                          `json:"name"`
	gameRule   request.GameRule                //规则
	gameFrame  base.GameFrame                  `json:"gameFrame"`
	users      map[uint]*response.GameFleeInfo //用户
	IsKill     bool                            //是否被杀
	BettingNum int64                           `json:"bettingNum"` //押注金额
	m          base.FleeBase
}

func (r *FleeKillRoom) GetUsers() map[uint]*response.GameFleeInfo {
	return r.users
}

// InitRoom 初始化房间
func (r *FleeKillRoom) InitRoom() {
	r.users = make(map[uint]*response.GameFleeInfo)
	r.IsKill = false
	r.BettingNum = 0
}

func (r *FleeKillRoom) Betting(data *response.GameFleeInfo, num int64) {
	r.BettingNum += num
	_, ok := r.users[data.UID]
	if ok {
		r.users[data.UID].BettingNum += num
	} else {
		r.users[data.UID] = data
	}
}
func (r *FleeKillRoom) Deduction(uid uint, num int64) {
	r.BettingNum -= num
	_, ok := r.users[uid]
	if ok {
		delete(r.users, uid)
	}
}

func NewFleeKillRoom(data request.FleeRoom, gameID uint, m base.FleeBase, gameService *service.GameService) *FleeKillRoom {
	r := &FleeKillRoom{
		ID:         data.ID,
		Name:       data.Name,
		BettingNum: data.BettingNum,
		users:      make(map[uint]*response.GameFleeInfo),
		m:          m,
	}
	if gameID == 2 {
		r.gameFrame = gameFlee.NewGameFrame(gameID, r, gameService)
	}
	return r
}
